//
//  DengLuViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/15.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "DengLuViewController.h"
#import "Header.h"
@interface DengLuViewController ()

@end

@implementation DengLuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"登陆";
    UIColor * color = [UIColor whiteColor];
    NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"dianchiliang"] forBarMetrics:UIBarMetricsDefault];
    
    UIBarButtonItem *bar=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"duigou"] style:UIBarButtonItemStylePlain target:self action:@selector(fanhui1)];
    
    self.navigationItem.leftBarButtonItem=bar;
    
    UIButton *zhuCeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    
    zhuCeBtn.frame =CGRectMake(0, 0, 40, 40);
    
    [zhuCeBtn setTitle:@"注册" forState:UIControlStateNormal];
    
    [zhuCeBtn addTarget:self action:@selector(ZhuCe:) forControlEvents:UIControlEventTouchUpInside];
    
    [zhuCeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    UIBarButtonItem *barZhu=[[UIBarButtonItem alloc]initWithCustomView:zhuCeBtn];
    
    self.navigationItem.rightBarButtonItem=barZhu;
    
    _textField1.clearButtonMode=UITextFieldViewModeWhileEditing;
     _textField2.clearButtonMode=UITextFieldViewModeWhileEditing;
    [_imagView1.layer setMasksToBounds:YES];
    [_imagView1.layer setBorderWidth:.6];
    
    [_imagView2.layer setMasksToBounds:YES];
    [_imagView2.layer setBorderWidth:.6];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)fanhui1{
    
    
    if (self.home) {
            AppDelegate * app = (AppDelegate *) [UIApplication sharedApplication].delegate;
        
            ViewController *vc =[[ViewController alloc]init];
        
            app.window.rootViewController = vc;

    }
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)ZhuCe:(UIButton *)sender {
    
    ZhuCeViewController * z=[[ZhuCeViewController alloc]init];
    z.title=@"注册";
    [self.navigationController pushViewController:z animated:YES];
   
}
-(IBAction)DengLu:(id)sender{
    
    BOOL isIPhone=[self validateMobile:self.textField1.text];
    if (!isIPhone)
    {
//        15990042951
        NSLog(@"---手机号码有问题");
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请输入正确的手机号码";
        hud.margin = 10.f;
       
        hud.removeFromSuperViewOnHide = YES;
       
        [hud hide:YES afterDelay:1];

        return;
    }
    if ([self.textField2.text isEqualToString:@""]||self.textField2==nil) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请输入正确的手机号码";
        hud.margin = 10.f;
        
        hud.removeFromSuperViewOnHide = YES;
        
        [hud hide:YES afterDelay:1];

         hud.labelText = @"请输入密码";
        return;
    }
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  
    
    [QingQiuShuJu DengLu:self.textField1.text Psw:self.textField2.text HuiChuan:^( NSDictionary * dic, BOOL isOK)
    {
        if (isOK) {
            
            if ([[dic valueForKey:@"status"] intValue] == 200)
            {
                
                NSLog(@"登陆成功");
                
                NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults setObject:[dic  valueForKey:@"msg"] forKey:@"msg"];
                
                NSDictionary * dic1= [dic valueForKey:@"data"];
                
                
                [userDefaults setObject:[dic1 valueForKey:@"id"]==[NSNull null]?@"":[dic1 valueForKey:@"id"] forKey:@"ID"];
                
                [userDefaults setObject:[dic1 valueForKey:@"nickname"]==[NSNull null]?@"":[dic1 valueForKey:@"nickname"] forKey:@"nickname"];
                
                NSLog(@"---%@",[[dic valueForKey:@"data"] valueForKey:@"nickname"]);
                
                 [userDefaults setObject:[dic1 valueForKey:@"age"]==[NSNull null]?@"":[dic1 valueForKey:@"age"] forKey:@"age"];
                
                 [userDefaults setObject:[dic1 valueForKey:@"birthday"]==[NSNull null]?@"":[dic1 valueForKey:@"birthday"] forKey:@"birthday"];
                 [userDefaults setObject:[dic1 valueForKey:@"created"]==[NSNull null]?@"":[dic1 valueForKey:@"created"] forKey:@"created"];
                
                [userDefaults setObject:[dic1 valueForKey:@"headImg"]==[NSNull null]?@"":[dic1 valueForKey:@"headImg"] forKey:@"headImg"];
                [userDefaults setObject:[dic1 valueForKey:@"phoneNum"]==[NSNull null]?@"":[dic1 valueForKey:@"phoneNum"] forKey:@"phoneNum"];
                [userDefaults setObject:[dic1 valueForKey:@"sex"]==[NSNull null]?@"":[dic1 valueForKey:@"sex"] forKey:@"sex"];
                
                [userDefaults setObject:[dic1 valueForKey:@"updated"]==[NSNull null]?@"":[dic1 valueForKey:@"updated"] forKey:@"updated"];
                [userDefaults synchronize];
                
                [hud hide:YES];
                
//                AppDelegate * app = (AppDelegate *) [UIApplication sharedApplication].delegate;
//                
//                ViewController *vc =[[ViewController alloc]init];
//                
//                app.window.rootViewController = vc;
                [self.view endEditing:YES];
                [self dismissViewControllerAnimated:YES completion:nil];
                
                
                
            }
            else{
                hud.mode = MBProgressHUDModeText;
                
                hud.labelText = @"账号不存在或密码错误";
                [hud hide:YES afterDelay:1];
                
            }
        }
        else{
            hud.mode = MBProgressHUDModeText;
            
            hud.labelText = @"服务器连接超时";
            [hud hide:YES afterDelay:1];

        }
   
      
    }];
    
}
- (BOOL)validateMobile:(NSString *)mobileNum
{
    if (mobileNum==nil) {
        return NO;
    }
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    return YES;
}
- (IBAction)WangJiMima:(id)sender{
    
    ZhuCeViewController * z=[[ZhuCeViewController alloc]init];
    z.title=@"修改密码";
    z.pswBool = YES;
    
    [self.navigationController pushViewController:z animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
